import { Injectable } from '@angular/core';
import {MsgResult} from '../../../models/msg-result';
import {Observable} from 'rxjs/Observable';
import {RequestUtil} from '../../../util/request-util';
import {API} from '../../../constants/constants';
import {DataAuditAPI} from "../data-audit-constants";

@Injectable()
export class DsService {

  constructor(
    private requestUtil: RequestUtil
  ) { }

  insertDataSource(ds): Observable<MsgResult> {
    const params = 'dsName=' + ds.dsName + '&dbName=' + ds.dbName + '&dbType=' + ds.dbType  + '&useType=' + ds.useType  + '&ip=' + ds.ip
      + '&port=' + ds.port  + '&userName=' + ds.userName  + '&passWord=' + ds.passWord  + '&isUse=' + ds.isUse;
    return this.requestUtil.post(DataAuditAPI.insertDataSource, params);
  }
  // 删除数据源
  deleteDataSource(id ): Observable<MsgResult> {
    return this.requestUtil.delete(DataAuditAPI.deleteDataSource+"/"+id+"?format=json")
  }
// 批量删除数据源
  deleteDataSources(ids): Observable<MsgResult> {
    const params = 'ids=' + ids ;
    return this.requestUtil.post(DataAuditAPI.deleteDataSources, params)
  }
  // 修改数据源
  updateDataSource(ds): Observable<MsgResult> {
    const params = 'dsName=' + ds.dsName + '&dbName=' + ds.dbName + '&dbType=' + ds.dbType  + '&useType=' + ds.useType  + '&ip=' + ds.ip
      + '&port=' + ds.port  + '&userName=' + ds.userName  + '&passWord=' + ds.passWord  + '&isUse=' + ds.isUse + '&id=' +ds.id;
    return this.requestUtil.put(DataAuditAPI.updateDataSource+ "/" + ds.id + "?format=json&" + params)
  }
  // 查询全部
  getDataSourceInfos(): Observable<MsgResult> {
    return this.requestUtil.get(DataAuditAPI.getDataSourceInfos)
  }

  // 根据id获取数据源
  getDataSourceByID(id): Observable<MsgResult> {
    return this.requestUtil.get(DataAuditAPI.getDataSourceById+"/"+id+"?format=json")
  }

}
